package com.dofast.bt.mvp.view.fragment.mian;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.adapter.TeaAdapter;
import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.base.BaseFragment;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.my.CollectStoreListActivity;
import com.dofast.bt.mvp.view.activity.shop.TeaDetailActivity;

import org.xutils.ex.DbException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * username:Created by hsj on 2016/12/7 0007.
 * class：
 * 类描述：茶铺
 */

public class ShopFragment extends BaseFragment implements AdapterView.OnItemClickListener {


    @BindView(R.id.title_tea_guanyin)
    TextView titleTeaGuanyin;
    @BindView(R.id.title_tea_red)
    TextView titleTeaRed;
    @BindView(R.id.title_tea_bitter)
    TextView titleTeaBitter;
    @BindView(R.id.title_tea_old)
    TextView titleTeaOld;
    @BindView(R.id.title_bar_layout)
    LinearLayout titleBarLayout;
    @BindView(R.id.txt_first)
    TextView txtFirst;
    @BindView(R.id.txt_recomend)
    TextView txtRecomend;
    @BindView(R.id.txt_popule)
    TextView txtPopule;
    @BindView(R.id.txt_tea_tool)
    TextView txtTeaTool;
    @BindView(R.id.txt_tea_package)
    TextView txtTeaPackage;
    @BindView(R.id.txt_tea_display)
    TextView txtTeaDisplay;
    @BindView(R.id.txt_host_shop)
    TextView txtHostShop;
    @BindView(R.id.lv_tea)
    ListView lvTea;
    //商品列表
    private List<GoodsBean> list = null;
    private TeaAdapter adapter = null;
    //商品id
    private int goodId = 0;
    //推荐id
    private int recommentId = 0;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_shop;
    }

    @Override
    protected void initView() {
        list = new ArrayList<GoodsBean>();

    }

    @Override
    protected void initData() {
        list.addAll(getData(recommentId));
        adapter = new TeaAdapter(list);
        lvTea.setAdapter(adapter);
        selectGuanyin();
        lvTea.setOnItemClickListener(this);
    }

    /**
     * 初始化标题栏
     */
    private void initTitle() {
        titleTeaGuanyin.setBackgroundColor(Color.rgb(49, 49, 49));
        titleTeaRed.setBackgroundColor(Color.rgb(49, 49, 49));
        titleTeaBitter.setBackgroundColor(Color.rgb(49, 49, 49));
        titleTeaOld.setBackgroundColor(Color.rgb(49, 49, 49));
        //设置字体大小
        titleTeaGuanyin.setTextSize(12);
        titleTeaRed.setTextSize(12);
        titleTeaBitter.setTextSize(12);
        titleTeaOld.setTextSize(12);
        //设置颜色
        titleTeaGuanyin.setTextColor(this.getResources().getColor(R.color.txt_gred_titile));
        titleTeaRed.setTextColor(this.getResources().getColor(R.color.txt_gred_titile));
        titleTeaBitter.setTextColor(this.getResources().getColor(R.color.txt_gred_titile));
        titleTeaOld.setTextColor(this.getResources().getColor(R.color.txt_gred_titile));

    }
    private  void initRecomand() {
        rootView.findViewById(R.id.txt_recomend).setSelected(false);
        rootView.findViewById(R.id.txt_popule).setSelected(false);
        rootView.findViewById(R.id.txt_tea_tool).setSelected(false);
        rootView.findViewById(R.id.txt_tea_package).setSelected(false);
        rootView.findViewById(R.id.txt_tea_display).setSelected(false);
        rootView.findViewById(R.id.txt_host_shop).setSelected(false);
    }

    /**
     * 铁观音
     */
    private void selectGuanyin() {
        initTitle();
        initRecomand();
        titleTeaGuanyin.setBackgroundResource(R.mipmap.bg_title_nornaml);
        //设置字体大小
        titleTeaGuanyin.setTextSize(14);
        //设置粗体
        TextPaint paint = titleTeaGuanyin.getPaint();
        paint.setFakeBoldText(true);
        //设置颜色
        titleTeaGuanyin.setTextColor(this.getResources().getColor(R.color.white));

        goodId = 0;
        recommentId = 10;
//        changeSelector(R.id.txt_recomend);
        refreshList();

    }


    /**
     * 发酵红茶
     */
    private void selectTeaRed() {
        initTitle();
        initRecomand();
        titleTeaRed.setBackgroundResource(R.mipmap.bg_title_nornaml);
        //设置字体大小
        titleTeaRed.setTextSize(14);
        //设置粗体
        TextPaint paint = titleTeaRed.getPaint();
        paint.setFakeBoldText(true);
        //设置颜色
        titleTeaRed.setTextColor(this.getResources().getColor(R.color.white));
        goodId = 1;
        recommentId = 11;
//        changeSelector(R.id.txt_recomend);
        refreshList();
    }

    /**
     * 苦瓜茶
     */
    private void selectTeaBitter() {
        initTitle();
        initRecomand();
        titleTeaBitter.setBackgroundResource(R.mipmap.bg_title_nornaml);
        //设置字体大小
        titleTeaBitter.setTextSize(14);
        //设置粗体
        TextPaint paint = titleTeaBitter.getPaint();
        paint.setFakeBoldText(true);
        //设置颜色
        titleTeaBitter.setTextColor(this.getResources().getColor(R.color.white));
        goodId = 2;
//        changeSelector(R.id.txt_recomend);
        recommentId = 12;
        refreshList();
    }

    /**
     * 成年老茶
     */
    private void selectTeaOld() {
        initTitle();
        initRecomand();
        titleTeaOld.setBackgroundResource(R.mipmap.bg_title_nornaml);
        //设置字体大小
        titleTeaOld.setTextSize(14);
        //设置粗体
        TextPaint paint = titleTeaOld.getPaint();
        paint.setFakeBoldText(true);
        //设置颜色
        titleTeaOld.setTextColor(this.getResources().getColor(R.color.white));
        goodId = 3;
        recommentId = 13;
        //changeSelector(R.id.txt_recomend);
        refreshList();
    }


    /**
     * @param recommendId 推荐id
     * @return List<GoodsBean> 茶列表
     */
    private List<GoodsBean> getData(int recommendId) {

        try {
            List<GoodsBean> tList = new MainModel().getGoodsByFlag(recommendId + 1);
            if (tList != null) {
                return tList;
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 刷新商品列表
     */
    private void refreshList() {
        if (!list.isEmpty()) {
            list.clear();
        }
        list.addAll(getData(recommentId));
        adapter = new TeaAdapter(list);
        lvTea.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.title_tea_guanyin, R.id.title_tea_red, R.id.title_tea_bitter, R.id.title_tea_old, R.id.title_bar_layout, R.id.txt_first, R.id.txt_recomend, R.id.txt_popule, R.id.txt_tea_tool, R.id.txt_tea_package, R.id.txt_tea_display, R.id.txt_host_shop})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_tea_guanyin:
                selectGuanyin();
                break;
            case R.id.title_tea_red:
                selectTeaRed();
                break;
            case R.id.title_tea_bitter:
                selectTeaBitter();
                break;
            case R.id.title_tea_old:
                selectTeaOld();
                break;
            case R.id.title_bar_layout:
                break;
            case R.id.txt_first:
                break;
            case R.id.txt_recomend:
                recommentId = 0;
                changeSelector(R.id.txt_recomend);
                //// TODO: 2016/12/12 0012  根据品牌和推荐类型获取列表
                refreshList();
                break;
            case R.id.txt_popule:
                recommentId = 1;
                changeSelector(R.id.txt_popule);
                //// TODO: 2016/12/12 0012  根据品牌和推荐类型获取列表
                refreshList();
                break;
            case R.id.txt_tea_tool:
                recommentId = 2;
                changeSelector(R.id.txt_tea_tool);
                //// TODO: 2016/12/12 0012  根据品牌和推荐类型获取列表
                refreshList();
                break;
            case R.id.txt_tea_package:
                recommentId = 3;
                changeSelector(R.id.txt_tea_package);
                //// TODO: 2016/12/12 0012  根据品牌和推荐类型获取列表
                refreshList();
                break;
            case R.id.txt_tea_display:
                recommentId = 4;
                changeSelector(R.id.txt_tea_display);
                //// TODO: 2016/12/12 0012  根据品牌和推荐类型获取列表
                refreshList();
                break;
            case R.id.txt_host_shop:

                //// TODO: 2016/12/12 0012  根据品牌和推荐类型获取列表

                Intent intent = new Intent(getActivity(), CollectStoreListActivity.class);
                intent.putExtra("hot", true);
                startActivity(intent);
                break;
        }
    }

    private void changeSelector(int resId) {
        initTitle();
        rootView.findViewById(R.id.txt_recomend).setSelected(false);
        rootView.findViewById(R.id.txt_popule).setSelected(false);
        rootView.findViewById(R.id.txt_tea_tool).setSelected(false);
        rootView.findViewById(R.id.txt_tea_package).setSelected(false);
        rootView.findViewById(R.id.txt_tea_display).setSelected(false);
        rootView.findViewById(R.id.txt_host_shop).setSelected(false);

        rootView.findViewById(resId).setSelected(true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO: 2016/12/12 0012 跳转到商品详情界面
        TeaDetailActivity.startTeaDetailActivity(getActivity(), list.get(position).getGoodsId());
    }
}
